package one.stand.service.user;

import com.google.common.collect.Lists;
import one.stand.dto.ConvertUtils;
import one.stand.dto.MenuTreeDto;
import one.stand.enums.LoginEnum;
import one.stand.mapper.MenuMapperExt;
import one.stand.mapper.MenuUserMapperExt;
import one.stand.model.AdminMenuModel;
import one.stand.model.Menu;
import one.stand.model.MenuUser;
import one.stand.util.OneListUtil;
import one.stand.util.SqlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author cjr
 */
@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuMapperExt menuMapperExt;
    @Autowired
    private MenuUserMapperExt menuUserMapperExt;
    @Autowired
    private TokenService tokenService;

    @Override
    @Transactional
    public void initCrm(Integer userId, boolean isAdmin) {
        menuUserMapperExt.deleteByMenu(userId, null);
        if (isAdmin) {
            menuUserMapperExt.insertInit(TYPE_CRM_APP, userId, null);
            menuUserMapperExt.insertInit(TYPE_CRM_PC, userId, null);
            menuUserMapperExt.insertInit(TYPE_JXS_APP, userId, null);
            menuUserMapperExt.insertInit(TYPE_CRN_APP_NEW, userId, null);
        } else {
            String scodeStr = SqlUtil.in(INIT_CRM_APP);
            menuUserMapperExt.insertInit(TYPE_CRM_APP, userId, scodeStr);
            scodeStr = SqlUtil.in(INIT_JXS_APP);
            menuUserMapperExt.insertInit(TYPE_JXS_APP, userId, scodeStr);
        }
    }

    @Override
    public void saveAdminRole(Integer adminId, Integer[] roleIds) {
        // 删除以前的权限
        menuMapperExt.delAdminMenu(adminId);
        List<AdminMenuModel> list = new ArrayList<>();
        for (Integer roid : roleIds) {
            AdminMenuModel model = new AdminMenuModel();
            model.setAdminId(adminId);
            model.setMenuId(roid);
            list.add(model);
        }
        menuMapperExt.insertAdminMenu(list);
    }

    @Override
    @Transactional
    public void remove(Integer userId) {
        menuUserMapperExt.deleteByMenu(userId, null);
    }

    @Override
    @Transactional
    public void update(Integer userId, List<Integer> menuIds, boolean isOpen) {
        if (isOpen) {
            add(userId, menuIds);
        } else {
            String ids = SqlUtil.in(menuIds);
            menuUserMapperExt.deleteByMenu(userId, ids);
        }
        tokenService.remove(userId, LoginEnum.CRM);
    }

    @Override
    @Transactional
    public void update(Integer userId, List<Integer> menuIds) {
        menuUserMapperExt.deleteByMenu(userId, null);
        add(userId, menuIds);
        tokenService.remove(userId, LoginEnum.CRM_PC);
        tokenService.remove(userId, LoginEnum.CRM_PC_ADMIN);
    }

    @Override
    public List<MenuTreeDto> getAll(Integer menuId, Integer menuType) {
        List<Menu> menus = menuMapperExt.selectAll(menuId, menuType);
        List<MenuTreeDto> menuTreeDtos = ConvertUtils.list(menus, menu -> MenuTreeDto.builder().menu(menu).build());
        menuTreeDtos = OneListUtil.group(menuTreeDtos,
                menu -> menu.getMenu().getParentId() == PARENT_ID,
                menu -> menu.getMenu().getMenuId(),
                menu -> menu.getMenu().getParentId(),
                (menu, child) -> {
                    if (OneListUtil.isNotEmpty(menu.getMenus())) {
                        menu.getMenus().add(child);
                    } else {
                        menu.setMenus(Lists.newArrayList(child));
                    }
                });
        return menuTreeDtos;
    }

    @Override
    public List<Menu> getOpenMenu(Integer userId, Integer type) {
        return menuUserMapperExt.selectOpen(userId, type);
    }

    @Override
    public List<Integer> getOpen(Integer userId) {
        List<Menu> openList = menuUserMapperExt.selectOpen(userId, null);
        return ConvertUtils.list(openList, Menu::getMenuId);
    }

    private void add(Integer userId, List<Integer> menuIds) {
        MenuUser menuUser;
        for (Integer menuId : menuIds) {
            menuUser = new MenuUser();
            menuUser.setUserId(userId);
            menuUser.setMenuId(menuId);
            menuUserMapperExt.insertSelective(menuUser);
        }
    }
}
